<template>
  <div class="container">
    <h2 class="handle-title">系统管理 > 修改机构信息</h2>
    <el-form ref="form" :rules="rules" :model="nowOrg" class="demo-ruleForm" label-width="80px" style="width:430px">
      <el-form-item label="机构名字" >
        <el-input v-model="nowOrg.orgname" style="width:190px" maxlength="20"></el-input>
      </el-form-item>
      <!--      <el-form-item label="角色状态">-->
      <!--        <el-select v-model="form.rolestate" placeholder="请选择角色状态">-->
      <!--          <el-option label="可用" value="1"></el-option>-->
      <!--          <el-option label="不可用" value="0"></el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="地址">
        <el-input  v-model="nowOrg.orgaddr"></el-input>
      </el-form-item>
      <el-form-item label="法定代表人">
        <el-input  v-model="nowOrg.orgcontacts"></el-input>
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input  v-model="nowOrg.orgphone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onUpdate('form')">修改</el-button>
        <el-button @click="goBack">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {selectOrgById} from "../../api/public/api";
import {updateOrg} from "../../api/public/api";

export default {
  name: '',
  data() {
    return {
      rules: {
        rolename: [
          {required: true, message: '请输入角色名称', trigger: 'blur'}
        ]
      },
      nowOrg: {
        orgname:'',
        orgaddr: '',
        orgcontacts:'',
        orgphone:'',
        orgid:''
      }
    }
  },
  mounted() {
    let that = this;
    //获取修改的数据id
    let id = that.$route.query.id
    //根据ID查询机构的详细信息
    selectOrgById(id).then((res) => {
      // console.log(res);
      that.nowOrg.orgname = res.data.data.orgname;
      that.nowOrg.orgaddr = res.data.data.orgaddr;
      that.nowOrg.orgcontacts = res.data.data.orgcontacts;
      that.nowOrg.orgphone = res.data.data.orgphone;
      that.nowOrg.orgid = res.data.data.orgid;
    })
  },
  methods: {
    //修改信息
    onUpdate(form) {
      let that = this;
      this.$refs[form].validate((value) => {
        // console.log(value)
        if (value) {
          let formData = new FormData();
          formData.append("orgname", that.nowOrg.orgname);
          formData.append("orgaddr", that.nowOrg.orgaddr);
          formData.append("orgcontacts", that.nowOrg.orgcontacts);
          formData.append("orgphone", that.nowOrg.orgphone);
          formData.append("orgid", that.nowOrg.orgid);
          updateOrg(formData).then((res) => {
            console.log(res);
            if (res.data.code == 1) {
              that.$message({
                message: "修改成功",
                type: "success",
              })
              that.$router.push("/organization")
            } else {
              this.$message({
                message: "修改失败",
                type: "info"
              });
            }
          })
        }
      })
    },
    //取消操作
    goBack() {
      this.$router.go(-1);
    },
  },
}
</script>

<style scoped>

</style>
